package com.apchernykh.rozhlasapp.ui.stations;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.rozhlasapp.RadioProgramAdapter;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.types.ApplicationModel;
import com.apchernykh.types.StationInfo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class ScheduleDetailsFragment extends com.apchernykh.rozhlasapp.ui.BaseDetailsFragment {
	/**
	 * Create a new instance of DetailsFragment, initialized to show the text at
	 * 'index'.
	 * 
	 * @param iStationInfo
	 * @param object
	 */

	public static ScheduleDetailsFragment newInstance(int iIndex) {
		ScheduleDetailsFragment aFragment = new ScheduleDetailsFragment();

		memorizeIndex(iIndex, aFragment);

		return aFragment;
	}

	@Override
	public View onCreateView(LayoutInflater iInflater, ViewGroup iContainer,
			Bundle oSavedInstanceState) {
		mDetailedView = null;
		if (iContainer != null
				&& RozhlasAppBaseFragmentActivity.mApplicationModel != null) {
			StationInfo aInfo = RozhlasAppBaseFragmentActivity.mApplicationModel
					.getSelectedStationInfo();
			mDetailedView = iInflater.inflate(R.layout.detailed, iContainer,
					false);
			if (aInfo != null) {
				setStationInfo(aInfo);
			}
			RozhlasAppBaseFragmentActivity.refreshPlaybackState(mDetailedView);
		}
		return mDetailedView;
	}

	public void setStationInfo(StationInfo iCurrentlyPlaying) {
		refreshStationPlayingNowInfo(this, iCurrentlyPlaying);
		TextView aShortName = (TextView) mDetailedView
				.findViewById(R.id.station_shortname);
		TextView aLongName = (TextView) mDetailedView
				.findViewById(R.id.station_name);
		if (aShortName == null || aLongName == null) {
			return;
		}
		if (iCurrentlyPlaying == null) {
			String aText = "Nedostatek informace";
			aShortName.setText(aText);
			aLongName.setText(aText);
			populateList(R.id.plying_now, null);
			populateList(R.id.plying_later, null);
		} else {
			aShortName.setText(iCurrentlyPlaying.mShortName);
			aLongName.setText(iCurrentlyPlaying.mShortName + ", "
					+ iCurrentlyPlaying.mShortdescription);
			populateList(R.id.plying_now, iCurrentlyPlaying.mPlayingNow);
			populateList(R.id.plying_later, iCurrentlyPlaying.mNextPrograms);
		}
		aShortName.invalidate();
		aLongName.invalidate();
	}

	// TODO: remove this copypaste
	void refreshStationPlayingNowInfo(
			final ScheduleDetailsFragment iScheduleDetailsFragment,
			final StationInfo iStationInfo) {
		if (iStationInfo == null) {
			return;
		}
		long aCurrentTime = System.currentTimeMillis();
		if (aCurrentTime > iStationInfo.mExpirationTime) {
			// ConditionalLog.e(iStationInfo.mShortName +
			// " refreshing "+String.valueOf(aCurrentTime)+" > "+String.valueOf(iStationInfo.mExpirationTime));
			iStationInfo.mExpirationTime = aCurrentTime + 60000;
			// Just before we refresh station info, we should set it's
			// expiration time to the future
			// to guarantee, that it won't be attempted for refresh in parallel.
			AsyncTask<Integer, Integer, Boolean> aAsyncDetailsExpirationTask = new AsyncTask<Integer, Integer, Boolean>() {
				@Override
				protected Boolean doInBackground(Integer... iParams) {
					try {
						iStationInfo.forceRefresh();
						if (iStationInfo.mStationId == RozhlasAppBaseFragmentActivity.mApplicationModel.mStationSelected) {
							RozhlasAppBaseFragmentActivity.mApplicationModel.mOnViewsExpiredListener
									.onViewExpired();
						}

						return true;
					} catch (Exception e) {
						e.printStackTrace();
					}
					return false;
				}

				@Override
				protected void onPostExecute(Boolean iUpdateSuccessful) {
					if (iUpdateSuccessful
							&& iStationInfo.mStationId == getShownIndex()) {
						iScheduleDetailsFragment.setStationInfo(iStationInfo);
						// ConditionalLog.e("refreshStationPlayingNowInfo",
						// iStationInfo.mShortName +
						// " refreshed "+String.valueOf(iStationInfo.mExpirationTime));
					} else {
						// ConditionalLog.d("onPostExecute",
						// iStationInfo.mShortName + " scrolled out");
					}
				}
			};
			if (RozhlasAppBaseFragmentActivity.mApplicationModel.mInetInfo
					.isDownloadAllowed()) {
				aAsyncDetailsExpirationTask.execute(0);
			}
		} else {
			ApplicationModel
					.notifyViewExpirationTime(iStationInfo.mExpirationTime);
		}
	}

	private void populateList(int iViewId,
			com.apchernykh.types.RadioSchedule iSchedule) {
		ListView aView = (ListView) mDetailedView.findViewById(iViewId);
		if (aView == null) {
			return;
		}

		if (iSchedule == null) {
			aView.setVisibility(View.GONE);
			return;
		}

		aView.setVisibility(View.VISIBLE);

		RadioProgramAdapter aAboutAdapter = new RadioProgramAdapter(
				aView.getContext(), R.layout.detailed_item, iSchedule.mPrograms);
		aView.setAdapter(aAboutAdapter);
		aView.invalidate();
	}

	public static void refresh(Fragment iFragment) {
		ScheduleDetailsFragment aDetails = (ScheduleDetailsFragment) iFragment;
		if (aDetails != null
				&& RozhlasAppBaseFragmentActivity.mApplicationModel
						.getSelectedStationInfo() != null) {
			aDetails.setStationInfo(RozhlasAppBaseFragmentActivity.mApplicationModel
					.getSelectedStationInfo());
		}

	}
}
